﻿/**
 * @file language.c
 *
 */

/*********************
 *      INCLUDES
 *********************/
#include "lv_watch.h"

#if USE_LV_WATCH_LANGUAGE != 0

#include <stdio.h>

/*********************
 *      DEFINES
 *********************/

/**********************
 *      TYPEDEFS
 **********************/

/**********************
 *  STATIC PROTOTYPES
 **********************/
static lv_obj_t * language_create(lv_obj_t * activity_obj);
static lv_language_obj_ext_t * language_get_ext(void);

/**********************
 *  STATIC VARIABLES
 **********************/
#if USE_LV_WATCH_LANG_ARABIC != 0
static lv_font_t * font_ar = NULL;
static lv_font_t * font_ch = NULL;
#endif
/**********************
 *      MACROS
 **********************/

/**********************
 *   GLOBAL FUNCTIONS
***********************/
void language_create_btn_action(lv_obj_t * btn, lv_event_t event)
{
    (void)btn;

    if(LV_EVENT_CLICKED != event) return;
    lv_watch_png_cache_all_free();

    lv_obj_t * obj = language_create(NULL);
    LV_ASSERT_MEM(obj);
    if(obj == NULL) return;
}

void language_switch_ttf(char * path)
{
#ifdef USE_FONT_TTF
    printf("language_switch_ttf\n");
    lv_freetype_font_destroy(&lv_font_ttf_20);
    lv_mem_free(lv_font_ttf_20.user_data);
    lv_freetype_font_init(&lv_font_ttf_20, path, 20);

    lv_freetype_font_destroy(&lv_font_ttf_30);
    lv_mem_free(lv_font_ttf_30.user_data);
    lv_freetype_font_init(&lv_font_ttf_30, path, 30);

    lv_freetype_font_destroy(&lv_font_ttf_12);
    lv_mem_free(lv_font_ttf_12.user_data);
    lv_freetype_font_init(&lv_font_ttf_12, path, 12);

    lv_freetype_font_destroy(&lv_font_ttf_14);
    lv_mem_free(lv_font_ttf_14.user_data);
    lv_freetype_font_init(&lv_font_ttf_14, path, 14);

    lv_freetype_font_destroy(&lv_font_ttf_16);
    lv_mem_free(lv_font_ttf_16.user_data);
    lv_freetype_font_init(&lv_font_ttf_16, path, 16);

    lv_freetype_font_destroy(&lv_font_ttf_40);
    lv_mem_free(lv_font_ttf_40.user_data);
    lv_freetype_font_init(&lv_font_ttf_40, path, 40);
#endif
}

/**********************
 *   STATIC FUNCTIONS
 **********************/
static void language_prepare_destory(lv_obj_t * activity_obj)
{
#if USE_LV_WATCH_LANG_ARABIC != 0
    if(font_ch) {
        lv_freetype_font_destroy(font_ch);
        lv_mem_free(font_ch);
        font_ch = NULL;
    }
    if(font_ar) {
        lv_freetype_font_destroy(font_ar);
        lv_mem_free(font_ar);
        font_ar = NULL;
    }
#endif
    lv_watch_png_cache_all_free();
}

static void setting_set_current_language(uint8_t lang_id)
{
    // nv write
    uint8_t nvdata = lang_id;
    UI_NV_Write_Req(NV_SECTION_UI_SETTINGS, 4, 1, &nvdata);

    lv_lang_set(lang_id);
    lv_obj_refresh_style(lv_scr_act(), LV_OBJ_PART_ALL, LV_STYLE_PROP_ALL);
}

static lv_language_obj_ext_t * language_get_ext(void)
{
    lv_obj_t * activity_obj = lv_watch_get_activity_obj(ACT_ID_LANGUAGE_SWITCH);
    if(activity_obj == NULL) return NULL;

    lv_obj_t * watch_obj;
    lv_watch_get_child_obj(activity_obj, lv_watch_obj_signal, &watch_obj);
    if(watch_obj == NULL) return NULL;

    if(watch_obj) {
        lv_language_obj_ext_t * ext = lv_obj_get_ext_attr(watch_obj);
        return ext;
    }
    return NULL;
}

static void languge_set_layout(lv_obj_t * obj)
{
     if(lv_obj_get_base_dir(obj) == LV_BIDI_DIR_LTR)
        lv_btn_set_layout(obj, LV_LAYOUT_COLUMN_LEFT);
    else if(lv_obj_get_base_dir(obj) == LV_BIDI_DIR_RTL)
        lv_btn_set_layout(obj, LV_LAYOUT_COLUMN_RIGHT);
    else
        lv_btn_set_layout(obj, LV_LAYOUT_COLUMN_LEFT);
}

static void language_chinese_btn_action(lv_obj_t * btn, lv_event_t event)
{
    if(event != LV_EVENT_CLICKED) return;

    lv_language_obj_ext_t * ext = language_get_ext();
    LV_ASSERT_MEM(ext);

    lv_checkbox_set_checked(ext->cb_ch, true);
    if(LANG_CH != lv_lang_act()) {
        if(LANG_EN == lv_lang_act()) {
            lv_checkbox_set_checked(ext->cb_en, false);
        }
#if USE_LV_WATCH_LANG_ARABIC != 0
        else {
            language_switch_ttf(LV_FONT_TTF);
            lv_checkbox_set_checked(ext->cb_ar, false);
        }
#endif
        setting_set_current_language(LANG_CH);
        lv_obj_align(ext->label_lang, lv_obj_get_parent(ext->label_lang), LV_ALIGN_IN_TOP_MID, 0, 10);
        languge_set_layout(lv_obj_get_parent(btn));
    }
}

static void language_english_btn_action(lv_obj_t * btn, lv_event_t event)
{
    if(event != LV_EVENT_CLICKED) return;

    lv_language_obj_ext_t * ext = language_get_ext();
    LV_ASSERT_MEM(ext);

    lv_checkbox_set_checked(ext->cb_en, true);
     if(LANG_EN != lv_lang_act()) {
         if(LANG_CH == lv_lang_act()) {
             lv_checkbox_set_checked(ext->cb_ch, false);
         }
#if USE_LV_WATCH_LANG_ARABIC != 0
         else if(LANG_AR == lv_lang_act()){
             language_switch_ttf(LV_FONT_TTF);
             lv_checkbox_set_checked(ext->cb_ar, false);
         }
#endif
         setting_set_current_language(LANG_EN);
         lv_obj_align(ext->label_lang, lv_obj_get_parent(ext->label_lang), LV_ALIGN_IN_TOP_MID, 0, 10);
         languge_set_layout(lv_obj_get_parent(btn));
     }
}

#if USE_LV_WATCH_LANG_ARABIC != 0
static void language_arabic_btn_action(lv_obj_t * btn, lv_event_t event)
{
    if(event != LV_EVENT_CLICKED) return;
    lv_language_obj_ext_t * ext = language_get_ext();
    LV_ASSERT_MEM(ext);

    lv_checkbox_set_checked(ext->cb_ar, true);
    if(LANG_AR != lv_lang_act()) {
        language_switch_ttf(LV_ARABIC_FONT_TTF);
        if(LANG_EN == lv_lang_act())
            lv_checkbox_set_checked(ext->cb_en, false);
        else if(LANG_CH == lv_lang_act())
            lv_checkbox_set_checked(ext->cb_ch, false);
        setting_set_current_language(LANG_AR);
        lv_obj_align(ext->label_lang, lv_obj_get_parent(ext->label_lang), LV_ALIGN_IN_TOP_MID, 0, 10);
        languge_set_layout(lv_obj_get_parent(btn));
    }
}
#endif

static lv_obj_t * language_create(lv_obj_t * activity_obj)
{
    if(activity_obj == NULL) {
        lv_watch_activity_ext_t activity_ext;
        memset(&activity_ext, 0, sizeof(lv_watch_activity_ext_t));
        activity_ext.actId = ACT_ID_LANGUAGE_SWITCH;
        activity_ext.create = language_create;
        activity_ext.prepare_destory = language_prepare_destory;
        activity_obj = lv_watch_creat_activity_obj(&activity_ext);
        LV_ASSERT_MEM(activity_obj);
        if(activity_obj == NULL) return NULL;
    }

    lv_obj_t * obj = lv_watch_obj_create(activity_obj);
    LV_ASSERT_MEM(obj);
    if(obj == NULL) return NULL;
    lv_language_obj_ext_t * ext = lv_obj_allocate_ext_attr(obj, sizeof(lv_language_obj_ext_t));
    LV_ASSERT_MEM(ext);

    ext->label_lang = lv_label_create(obj, NULL);
    lv_obj_add_style(ext->label_lang, LV_LABEL_PART_MAIN, &lv_watch_font20_gray);
    lv_label_set_align(ext->label_lang, LV_LABEL_ALIGN_CENTER);
    lv_obj_set_click(ext->label_lang, false);
    lv_label_set_text_id(ext->label_lang, WATCH_TEXT_ID_PLEASE_SELECT_LANGUAGE);
    lv_obj_align(ext->label_lang, obj, LV_ALIGN_IN_TOP_MID, 0, 10);

    lv_obj_t * cont = lv_cont_create(obj, NULL);
    lv_obj_add_style(cont, LV_OBJ_PART_MAIN, &lv_style_transp_fit);
    languge_set_layout(cont);
    lv_obj_set_width(cont, 120);
    lv_obj_align(cont, NULL, LV_ALIGN_CENTER, -10, -15);
    lv_cont_set_fit2(cont, LV_FIT_NONE, LV_FIT_TIGHT);
    lv_obj_set_click(cont, false);

    ext->cb_ch = lv_checkbox_create(cont, NULL);
    lv_checkbox_set_text(ext->cb_ch, "简体中文");
    lv_obj_clean_style_list(ext->cb_ch, LV_CHECKBOX_PART_BULLET);
    lv_obj_add_style(ext->cb_ch, LV_CHECKBOX_PART_BULLET, &lv_watch_cb_styles);
    lv_obj_set_event_cb(ext->cb_ch, language_chinese_btn_action);
    lv_watch_obj_add_element(ext->cb_ch);

    ext->cb_en = lv_checkbox_create(cont, ext->cb_ch);
    lv_checkbox_set_text(ext->cb_en, "English");
    lv_obj_set_event_cb(ext->cb_en, language_english_btn_action);
    lv_watch_obj_add_element(ext->cb_en);

#if USE_LV_WATCH_LANG_ARABIC != 0
    ext->cb_ar = lv_checkbox_create(cont, ext->cb_ch);
    lv_checkbox_set_text(ext->cb_ar, "بالعربية");
    lv_obj_set_event_cb(ext->cb_ar, language_arabic_btn_action);
    lv_watch_obj_add_element(ext->cb_ar);

    font_ar = lv_mem_alloc(sizeof(lv_font_t));
    lv_freetype_font_init(font_ar, LV_ARABIC_FONT_TTF, 20);
    lv_checkbox_ext_t * ar_ext = lv_obj_get_ext_attr(ext->cb_ar);
    lv_obj_set_style_local_text_font(ar_ext->label, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, font_ar);

    font_ch = lv_mem_alloc(sizeof(lv_font_t));
    lv_freetype_font_init(font_ch, LV_FONT_TTF, 20);
    lv_checkbox_ext_t * ch_ext = lv_obj_get_ext_attr(ext->cb_ch);
    lv_obj_set_style_local_text_font(ch_ext->label, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, font_ch);
#endif
    if(LANG_CH == lv_lang_act()) {
        lv_checkbox_set_checked(ext->cb_ch, true);
#if USE_LV_WATCH_LANG_ARABIC != 0
    } else if(LANG_AR == lv_lang_act()) {
        lv_checkbox_set_checked(ext->cb_ar, true);
#endif
    } else {
        lv_checkbox_set_checked(ext->cb_en, true);
    }

    return obj;
}

#endif /*USE_LV_WATCH_LANGUAGE*/
